package com.stephentuso.welcome;

import com.stephentuso.welcome.util.PageView;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.render.Canvas;
import ohos.app.Context;
import ohos.utils.PacMap;

/**
 * Created by stephentuso on 1/24/16.
 */
public class WelcomeFullScreenParallaxPageView extends PageView implements Component.DrawTask, WelcomePage.OnChangeListener {

    public static final String KEY_LAYOUT_ID = "drawable_id";
    public static final String KEY_START_FACTOR = "start_factor";
    public static final String KEY_END_FACTOR = "end_factor";
    public static final String KEY_PARALLAX_RECURSIVE = "parallax_recursive";

    private StackLayout frameLayout = null;

    private float startFactor = 0.2f;
    private float endFactor = 1.0f;
    private float parallaxInterval = 0f;
    private boolean parallaxRecursive = false;

    public static WelcomeFullScreenParallaxPageView newInstance(Context context, int layoutId, float startParallaxFactor, float endParallaxFactor,
                                                                boolean parallaxRecursive) {
        PacMap args = new PacMap();
        args.putIntValue(KEY_LAYOUT_ID, layoutId);
        args.putFloatValue(KEY_START_FACTOR, startParallaxFactor);
        args.putFloatValue(KEY_END_FACTOR, endParallaxFactor);
        args.putBooleanValue(KEY_PARALLAX_RECURSIVE, parallaxRecursive);
        WelcomeFullScreenParallaxPageView pageView = new WelcomeFullScreenParallaxPageView(context, args);
        return pageView;
    }

    public WelcomeFullScreenParallaxPageView(Context context, PacMap args) {
        super(context);
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_wel_page_view_parallax_full_screen, this, true);

        frameLayout = (StackLayout) component.findComponentById(ResourceTable.Id_wel_parallax_frame);

        startFactor = args.getFloatValue(KEY_START_FACTOR, startFactor);
        endFactor = args.getFloatValue(KEY_END_FACTOR, endFactor);
        parallaxRecursive = args.getBooleanValue(KEY_PARALLAX_RECURSIVE, parallaxRecursive);

        LayoutScatter.getInstance(context).parse(args.getIntValue(KEY_LAYOUT_ID), frameLayout, true);
        this.addDrawTask(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        parallaxInterval = (endFactor - startFactor)/(WelcomeUtils.calculateParallaxLayers(frameLayout.getComponentAt(0), parallaxRecursive) - 1);
    }

    @Override
    public void onWelcomeScreenPageScrolled(int pageIndex, float offset, int offsetPixels) {
        /*if (frameLayout != null) {
            WelcomeUtils.applyParallaxEffect(frameLayout.getComponentAt(0), parallaxRecursive, offsetPixels, startFactor, parallaxInterval);
        }*/
    }

    @Override
    public void onWelcomeScreenPageSelected(int pageIndex, int selectedPageIndex) {
        //Not used
    }

    @Override
    public void onWelcomeScreenPageScrollStateChanged(int pageIndex, int state) {
        //Not used
    }

}